﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BOAcademias;
using TOAcademias;
using System.IO;
using AjaxControlToolkit;
using System.Collections.Specialized;
using System.Data;

public partial class TunkiFiles_Reportes_ReporteRequerimientoxSeccion : System.Web.UI.Page
{
    BOAcademia boAcademia = new BOAcademia();
    BOInstructor boInstructor = new BOInstructor();
    BOSeccion boSeccion = new BOSeccion();
    BOReporte boReporte = new BOReporte();

    protected void Page_Load(object sender, EventArgs e)
    {


    }


    //Llenar Academias
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static AjaxControlToolkit.CascadingDropDownNameValue[] GetcboAcademia(string knownCategoryValues, string category)
    {

        BOAcademia boAcademia = new BOAcademia();
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        
        foreach (TOAcademia dr in boAcademia.obtenerAcademias())
        {
            values.Add(new CascadingDropDownNameValue(
            dr.Des_Academia,
            dr.Cod_Academia));
        }

        return values.ToArray();

    }

    //Llenar Instructores
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static AjaxControlToolkit.CascadingDropDownNameValue[] GetcboInstructor(string knownCategoryValues, string category)
    {

        StringDictionary kv =
        CascadingDropDown.ParseKnownCategoryValuesString(
        knownCategoryValues);

        String CodAcademia=kv["Academia"];

        BOInstructor boInstructor = new BOInstructor();                
        
        List<CascadingDropDownNameValue> values =
        new List<CascadingDropDownNameValue>();

        //foreach (TOInstructor dr in boInstructor.obtenerInstructorsxCodAcademia(CodAcademia))
        foreach (TOInstructor dr in boInstructor.obtenerInstructors())
        {
            values.Add(new CascadingDropDownNameValue(
            dr.NombreCompleto,
            dr.Codigo));
        }

        return values.ToArray();

    }

    //Llenar Secciones
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static CascadingDropDownNameValue[] GetcboSeccion(string knownCategoryValues, string category)
    {
        StringDictionary kv =
        CascadingDropDown.ParseKnownCategoryValuesString(
        knownCategoryValues);

        String CodInstructor = kv["Instructor"];

        BOSeccion boSeccion = new BOSeccion();

        List<CascadingDropDownNameValue> values =
        new List<CascadingDropDownNameValue>();

        foreach (TOSeccion dr in boSeccion.obtenerSeccionesActivasxInstructor(CodInstructor))
        {
            values.Add(new CascadingDropDownNameValue(
            dr.Cod_seccion,
            dr.Cod_seccion));
        }

        return values.ToArray();
    }

        

    protected void imgGenerarReporte_Click(object sender, ImageClickEventArgs e)
    {
        Session["Reporte"] = null;
        Session["TipoReporte"] = null;
        Session["Academia"] = null;
        Session["Anio"] = null;
        Session["Ciclo"] = null;
        Session["Instructor"] = null;
        Session["Seccion"] = null;
        Server.Transfer("VisorReporte.aspx", true);
    }

    protected void imgExportarExcel_Click(object sender, ImageClickEventArgs e)
    {
        GridView1.DataSource = boReporte.reporteRequerimientoxSeccion();
        GridView1.DataBind();
        //PrepareGridViewForExport(GridView1);
        ExportGridView(GridView1);
    }

    private void ExportGridView(GridView Grid)
    {
        string attachment = "attachment; filename=Reporte.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        Grid.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    private void PrepareGridViewForExport(Control gv)
    {
        LinkButton lb = new LinkButton();
        Literal l = new Literal();
        string name = String.Empty;

        for (int i = 0; i < gv.Controls.Count; i++)
        {
            if (gv.Controls[i].GetType() == typeof(LinkButton))
            {
                l.Text = (gv.Controls[i] as LinkButton).Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(DropDownList))
            {
                l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            else if (gv.Controls[i].GetType() == typeof(CheckBox))
            {
                l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }
            if (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }
    }




}
